package com.example.mobifin_busticket.models;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.mobifin_busticket.AppData;

public class PassengerDetails implements Serializable{

	String EmailId, travellerName, age, gender, phno, title, primary,
			availableTripId, boardingPointId, source, destination, strJSON;
	ArrayList<String> ladiesSeat;
	ArrayList<String> inventoryItems;
	ArrayList<String> seatName;
	ArrayList<Integer> fare;
	ArrayList<PassengerDetails> passengerList;

	public String getstrJSON() {
		return strJSON;
	}

	public void setstrJSON(String strJSON) {
		this.strJSON = strJSON;
	}

	public String getEmailId() {
		return EmailId;
	}

	public void setEmailId(String EmailId) {
		this.EmailId = EmailId;
	}

	public String getphno() {
		return phno;
	}

	public void setphno(String phno) {
		this.phno = phno;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String gettravellerName() {
		return travellerName;
	}

	public void settravellerName(String travellerName) {
		this.travellerName = travellerName;
	}

	public String getage() {
		return age;
	}

	public void setage(String age) {
		this.age = age;
	}

	public String getgender() {
		return gender;
	}

	public void setgender(String gender) {
		this.gender = gender;
	}

	public ArrayList<String> getladiesSeat() {
		return ladiesSeat;
	}

	public void setladiesSeat(ArrayList<String> ladiesSeat) {
		this.ladiesSeat = ladiesSeat;
	}
	
	
	public String getprimary() {
		return primary;
	}

	public void setprimary(String primary) {
		this.primary = primary;
	}

	public String getavailableTripId() {
		return availableTripId;
	}

	public void setavailableTripId(String availableTripId) {
		this.availableTripId = availableTripId;
	}

	public String getboardingPointId() {
		return boardingPointId;
	}

	public void setboardingPointId(String boardingPointId) {
		this.boardingPointId = boardingPointId;
	}

	public String getsource() {
		return source;
	}

	public void setsource(String source) {
		this.source = source;
	}

	public ArrayList<String> getseatName() {
		return seatName;
	}

	public void setseatName(ArrayList<String> seatName) {
		this.seatName = seatName;
	}

	public ArrayList<Integer> getfare() {
		return fare;
	}

	public void setfare(ArrayList<Integer> fare) {
		this.fare = fare;
	}

	public String getdestination() {
		return destination;
	}

	public void setdestination(String destination) {
		this.destination = destination;
	}

	public ArrayList<PassengerDetails> getpassengerList() {
		return passengerList;
	}

	public void setpassengerList(ArrayList<PassengerDetails> passengerList) {
		this.passengerList = passengerList;

	}

	public String toJSON(PassengerDetails passengerModel) {
		int size = passengerModel.getpassengerList().size();
		System.out.println("List size from json:" + size);

		JSONObject tripDetailsObj = new JSONObject();

		try {

			tripDetailsObj.put("availableTripId",
					passengerModel.getavailableTripId());
			tripDetailsObj.put("boardingPointId",
					passengerModel.getboardingPointId());
			tripDetailsObj.put("source", passengerModel.getsource());
			JSONArray passengerArray = new JSONArray();
			JSONObject passengerSubObj;
			for (int i = 0; i < passengerModel.getpassengerList().size(); i++) {
				if (i == 0)
					passengerModel.setprimary("true");

				else
					passengerModel.setprimary("false");

				passengerSubObj = new JSONObject();
				JSONObject passengerObj = new JSONObject();
				passengerObj.put("name",
						passengerModel.getpassengerList().get(i)
								.gettravellerName());
				passengerObj.put("gender", passengerModel.getpassengerList()
						.get(i).getgender());
				passengerObj.put("title", passengerModel.getpassengerList()
						.get(i).getTitle());
				passengerObj.put("email", passengerModel.getpassengerList()
						.get(i).getEmailId());
				passengerObj.put("age", passengerModel.getpassengerList()
						.get(i).getage());

				passengerObj.put("primary", passengerModel.getprimary());
				passengerObj.put("mobile", passengerModel.getpassengerList()
						.get(i).getphno());

				passengerSubObj.put("fare", passengerModel.getfare().get(i));
				passengerSubObj.put("ladiesSeat", passengerModel
						.getladiesSeat().get(i));
				passengerSubObj.put("seatName", passengerModel.getseatName()
						.get(i));
				passengerSubObj.put("passenger", passengerObj);
				passengerArray.put(passengerSubObj);
			}

			tripDetailsObj.put("destination", passengerModel.getdestination());
			tripDetailsObj.put("inventoryItems", passengerArray);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String jsonStr = tripDetailsObj.toString();
		AppData.JSONObj = tripDetailsObj.toString();
		passengerModel.setstrJSON(jsonStr);
		System.out.println("jsonString: " + jsonStr);
		return jsonStr;
	}
}
